<?php

declare(strict_types=1);

namespace App\Model\Act;

use App\Helpers\AliyunUtil;
use App\Model\Gift;
use Hyperf\DbConnection\Model\Model;

/**
 * 签到记录
 */
class SigninLog extends Model
{
    protected $table = 'act_signin_log';

    const GIFT_ID = 51;
    const REWARD_GIFT = [
        1 => ['gift_id' => self::GIFT_ID, 'gift_num' => 1],
        ['gift_id' => self::GIFT_ID, 'gift_num' => 1],
        ['gift_id' => self::GIFT_ID, 'gift_num' => 1],
        ['gift_id' => self::GIFT_ID, 'gift_num' => 3],
        ['gift_id' => self::GIFT_ID, 'gift_num' => 1],
        ['gift_id' => self::GIFT_ID, 'gift_num' => 1],
        ['gift_id' => 60, 'gift_num' => 1],
    ];

    const GIFT_ID_DEV = 54;
    const REWARD_GIFT_DEV = [
        1 => ['gift_id' => self::GIFT_ID_DEV, 'gift_num' => 6],
        ['gift_id' => self::GIFT_ID_DEV, 'gift_num' => 2],
        ['gift_id' => self::GIFT_ID_DEV, 'gift_num' => 3],
        ['gift_id' => self::GIFT_ID_DEV, 'gift_num' => 3],
        ['gift_id' => self::GIFT_ID_DEV, 'gift_num' => 4],
        ['gift_id' => self::GIFT_ID_DEV, 'gift_num' => 4],
        ['gift_id' => 55, 'gift_num' => 1],
    ];

    static function getRewardGiftConf()
    {
        return check_dev_env() ? self::REWARD_GIFT_DEV : self::REWARD_GIFT;
    }

    /**
     * 奖励列表
     */
    static function packRewardList()
    {
        $data = [];
        $giftConf = self::getRewardGiftConf();
        $giftIds = array_unique(array_column($giftConf, 'gift_id'));
        $giftInfo = Gift::getBatchInfo($giftIds);
        foreach ($giftConf as $day => $item) {
            $giftItem = $giftInfo[$item['gift_id']];
            if ($day == 7) {
                $name = '神秘大礼';
                $icon = AliyunUtil::imageResize(AliyunUtil::IMG_HOST_V2 . '/images/7157d534e31cb7108413e9828f595f72022.png', 200, 200);
            } else {
                $name = $item['gift_num'] . $giftItem['name'];
                $icon = AliyunUtil::imageResize(AliyunUtil::IMG_HOST_V2 . $giftItem['icon'], 200, 200);
            }
            $data[] = [
                'icon' => $icon,
                'name' => $name,
                'num' => $item['gift_num'],
                'day' => $day
            ];
        }
        return $data;
    }

    /**
     * 最新签到记录
     */
    static function getLastItem($userId = 0)
    {
        $where = [
            'user_id' => $userId,
            'flag' => 1,
        ];
        return self::where($where)->orderBy('id', 'desc')->first();
    }

    /**
     * 今天是否签到
     */
    static function checkToday($lastItem = null)
    {
        if (!$lastItem) return 0;
        $signDt = date('Y-m-d', strtotime((string)$lastItem->created_at));
        if ($signDt == date('Y-m-d')) return 1;
        return 0;
    }

    /**
     * 获取当前连续签到的天数
     */
    static function getSignInDay($lastItem = null)
    {
        if (!$lastItem) return 0;
        $signDt = date('Y-m-d', strtotime((string)$lastItem->created_at));
        if ($signDt < date("Y-m-d", strtotime('-1 day'))) return 0;
        return $lastItem->signin_day;
    }

    /**
     * 签到
     */
    static function doSignIn($userId = 0, $checkLast = null, $rewardGiftId = 1, $rewardGiftNum = 1)
    {
        $model = new self();
        $model->user_id = $userId;
        $model->reward_gift_id = $rewardGiftId;
        $model->reward_gift_num = $rewardGiftNum;
        $signinDay = self::getSignInDay($checkLast) + 1;
        $model->signin_day = $signinDay > 7 ? 1 : $signinDay;
        return $model->saveOrFail() ? 1 : 0;
    }
}
